package java6681.sort.list;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Junichiro
 * @description
 * @date 2021-04-05 23:55
 **/

public class ListSort6681Controller {
    @FXML
    private RadioButton rb1;

    @FXML
    private RadioButton rb2;

    @FXML
    private RadioButton rb3;

    @FXML
    private RadioButton rb4;

    @FXML
    private ListView<String> lvShow;

    @FXML
    private void initialize() {
        btnTest();
        btnSecond();
        btnThree();
        btnFour();
    }

    /**
     * 功能：使用自然排序 按成绩降序 初始化显示
     */
    public void btnTest() {
        rb1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (rb1.isSelected()) {
                    ObservableList<String> items = lvShow.getItems();
                    /*使用自然排序 按成绩降序*/
                    List<Course6681> list = starting();

                    /*先清空再显示*/
                    lvShow.getItems().clear();

                    Collections.sort(list);
                    for (Course6681 course6681 : list) {
                        items.add(String.valueOf(course6681));

                    }

                }
            }
        });
    }

    /**
     * 功能：点击按钮 使用比较器 按学期升序
     */
    public void btnSecond() {
        rb2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                List<Course6681> list = starting();
                lvShow.getItems().clear();
                Collections.sort(list, new Comparator<Course6681>() {
                    @Override
                    public int compare(Course6681 c1, Course6681 c2) {
                        String cSb1 = c1.getTerm().substring(0, 1);
                        String cSb2 = c2.getTerm().substring(0, 1);
                        int num = Integer.parseInt(cSb1) - Integer.parseInt(cSb2);
                        int num2 = num == 0 ? c1.getTerm().substring(1, 2).compareTo(c2.getTerm().substring(1, 2)) : num;
                        return num2;
                    }
                });
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }

    /**
     * 功能：点击按钮 使用比较器 按学分升序
     */
    public void btnThree() {
        rb3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                /*使用比较器排序 按学分升序 */
                List<Course6681> list = starting();
                lvShow.getItems().clear();
                Collections.sort(list, new SorterByCredit6681());
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }

    /**
     * 功能：点击按钮 使用比较器 按学期、学分多关键字排序（升序）
     */
    public void btnFour() {
        rb4.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                ObservableList<String> items = lvShow.getItems();
                lvShow.getItems().clear();
                List<Course6681> list = courseList();
                for (Course6681 course6681 : list) {
                    items.add(String.valueOf(course6681));
                }
            }
        });
    }

    /**
     * 功能：比较器
     *
     * @return list List<Course6681>返回比较好的内容
     */
    public static List<Course6681> courseList() {
        List<Course6681> list = starting();

        Collections.sort(list, new Comparator<Course6681>() {
            @Override
            public int compare(Course6681 c1, Course6681 c2) {
                String cSb1 = c1.getTerm().substring(0, 1);
                String cSb2 = c2.getTerm().substring(0, 1);
                int num = Integer.parseInt(cSb1) - Integer.parseInt(cSb2);
                int num2 = num == 0 ? c1.getTerm().substring(1, 2).compareTo(c2.getTerm().substring(1, 2)) : num;
                int num3 = num2 == 0 ? c1.getCredit() - c2.getCredit() : num2;
                return num3;
            }
        });
        return list;
    }

    /**
     * 功能：初始化Course6681对象
     *
     * @return list 返回 Course6681对象
     */
    public static List<Course6681> starting() {
        List<Course6681> list = new ArrayList<>();
        Course6681 c1 = new Course6681("Java", 3, 89, "1下", "A001");
        Course6681 c2 = new Course6681("Python", 2, 92, "2上", "A002");
        Course6681 c3 = new Course6681("C++", 2, 78, "2下", "A003");
        Course6681 c4 = new Course6681("HTML+CSS", 2, 69, "3上", "A004");
        Course6681 c5 = new Course6681("计算机网络", 4, 79, "1上", "A005");
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        return list;
    }
}

//    /**
//     * 功能：点击按钮 使用自然排序 按成绩降序
//     */
//    public void btnOne() {
//        ObservableList<String> items = lvShow.getItems();
//        List<Course6681> list = starting();
//
//        /*先清空再显示*/
//        lvShow.getItems().clear();
//
//        Collections.sort(list);
//        for (Course6681 course6681 : list) {
//            items.add(String.valueOf(course6681));
//
//        }
//    }